var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,$ = layui.jquery,laydate = layui.laydate;
layui.use(['tableEdit',"excel"], function () {
    let tableEdit = layui.tableEdit;
    let layer = layui.layer,
        excel = layui.excel;

    lay('.starttime').each(function () {
        laydate.render({
            elem: this
        });
    });

    function getAllJson(jsons) {
        let tmpitem=[]
        for (let item of jsons) {
            let tmparr={}
            tmparr["value"]=item["code"];
            tmparr["name"]=item["name"];
            if(item["users"]){
                for(let it of item["users"]){
                    item["list"].push(it)
                }
            }
            if(item["list"] && item["list"].length){
                tmparr["children"]=  getAllJson(item["list"]);
                tmpitem.push(tmparr)
            }else{
                tmpitem.push(tmparr)
            }

        }
        return tmpitem
    }
    function getxmselect(el,name, data) {
        return xmSelect.render({
            el: el,
            name: name,
            tree: {
                show: true,
                showLine: true,
                showFolderIcon:true,
                indent: 20,
                expandedKeys:false
            },
            height: 'auto',
            data: data,
            on: function ({ arr, change, isAdd }) {
                return this
            }
        })
    }



    let taxIncludedList=[];
    layer.ready(function () {
        //供应商
        $.ajax({
            url: "customer2/getAllInspectUnit",
            success: function (res) {
                if (res.status == 200) {
                    $.each(res.data, function (index, item) {
                        $("#supplierCode").append(new Option(item.name, item.code));
                    });
                    form.render('select');
                } else {
                    layer.msg(res.msg, {icon: 5});
                }
            }
        });


        //是否含税
        let taxIncluded = [
            {name: "含税出厂价", value: "1"},
            {name: "未税出厂价", value: "2"},
            {name: "含税FOB价", value: "3"},
            {name: "未税FOB价", value: "4"},
        ];
        taxIncludedList=getxmselect("#taxIncluded", "taxIncluded", taxIncluded);

    });
    form.render();//表单刷新

    document.getElementById('clear').onclick = function(){
        taxIncludedList.setValue([ ]);
    };


    let initData = [];
    table.render({
        elem: '#statisticsSelect'
        , data: initData
        , toolbar: '#statisticsToolbar'
        ,totalRow: true
        , cols: [[
              {field: 'numbers',width: 50,type:'numbers',title:'序号', totalRowText: '合计'}
            , {field: 'supplierName',  title: '供应商名称',width: 400, align: 'center'}
            , {field: 'taxIncluded', title: '是否含税', sort: true, width: 200, align: 'center'}
            , {field: 'signingDateStr', title: '签约日期', sort: true, width: 200, align: 'center'}
            , {field: 'productCode', title: '产品编码', width: 200, align: 'center'}
            , {field: 'specificationCode', title: '规格编码', width: 200, align: 'center'}
            , {field: 'chineseDesc', title:'中文描述',width: 200, align: 'center'}
            , {field: 'englishDesc', title:'英文描述',width: 200, align: 'center'}
            , {field: 'productUnitPrice', title:'产品单价',sort:true, width: 140, align: 'center'}
            , {field: 'purchaseCost', title: '采购成本', sort: true, width: 140, align: 'center', totalRow: true}
            , {field: 'taxIncludedPrice', title:'含税出厂价', width: 140, align: 'center', sort: true}
            , {field: 'untaxedPrice', title:'未税出厂价', width: 140, align: 'center', sort: true}
            , {field: 'taxIncludedFob', title:'含税FOB价', width: 140, align: 'center', sort: true}
            , {field: 'untaxedFob', title:'未税FOB价', width: 140, align: 'center', sort: true}
        ]]
        , id: 'statisticsTableId'
        , height: $(document).height() - $('#statisticsSelect').offset().top - 50
        , page: true
        , limit: 300
        , limits: [300, 600, 900]
    });

    form.on('submit(submitform)', function (data) {
        table.reload('statisticsTableId', {
            page: {
                limit:300,
                curr: 1
            }
            , url: 'productmaininfo/statistics/productNotEnteredPurchaseStatisticsListByPage'
            , where: data.field
        }, 'data');
        return false;
    });

    form.on('submit(exportData)', function (queryReq) {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: queryReq.field,
            url:"productmaininfo/productNotEnteredPurchaseExport",
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 120,
                        'X': 120
                    }, 80);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '产品未录入单价下采购单统计.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });

});